iT邦幫忙

2023 iThome 鐵人賽

DAY 24
1
Software Development

Spring Boot 零基礎入門系列 第 24

Spring Boot 零基礎入門 (25) - 資料庫連線設定

  • 分享至 

  • xImage
  •  

在 pom.xml 中載入 Spring JDBC 的功能

如果想要在 Spring Boot 中使用 Spring JDBC 的功能的話,首先會需要在 pom.xml 檔案中新增下方的程式,這樣才能夠將 Spring JDBC 的功能、以及 MySQL 資料庫的 driver 給載入進來。

因此大家可以先打開左邊側邊欄中的 pom.xml 檔案,然後在第 29 行~第 37 行的地方,添加下面的程式:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.33</version>
</dependency>

添加好上述的程式之後,此時在 pom.xml 的右上角會出現一個 M 符號,這時記得要點擊一下這個 M 符號,這樣才能夠更新 Spring Boot 程式,把 Spring JDBC 的功能、以及 MySQL 資料庫的 driver,一起給載入進來。

25-2.png

在 application.properties 中設定資料庫連線資訊

載入好 Spring JDBC 和 MySQL 的 driver 之後,接著我們就可以在 Spring Boot 中設定 MySQL 資料庫的連線,這樣我們後續就能夠在 Spring Boot 程式裡面,去操作 MySQL 資料庫中的數據了。

而要在 Spring Boot 中設定資料庫的連線資訊的話,就只要先打開 application.properties 檔案,並且在裡面添加下列的程式,這樣就能完成 MySQL 資料庫的連線設定了!

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjdbc?serverTimezone=Asia/Taipei&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=springboot

上一篇
Spring Boot 零基礎入門 (24) - Spring JDBC 簡介
下一篇
Spring Boot 零基礎入門 (26) - Spring JDBC 的用法(上)- 執行 INSERT、UPDATE、DELETE SQL
系列文
Spring Boot 零基礎入門29
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0

不愧是近幾年的教學xD,下面的這個依賴都是最新的

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.33</version>
</dependency>

這幾個禮拜找過不知道多少個 Spring 框架相關的教學
很多都因是較舊的文章,因此程式碼也是比較舊的,導致這段一直爆錯
我找了超多國外的影片和文章才找到上方這個
沒想到這個系列的文章就有附上
真的是太晚看到這篇文章啦!

我要留言

立即登入留言